package com.xxxt.wsc.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.xxxt.dao.PracticeMapper;
import com.xxxt.entity.Practice;
import com.xxxt.utils.DateUtil;
import com.xxxt.utils.FileUploadUtil;
import com.xxxt.wsc.service.PracticeService;

/**
 * @description:实习记录相关操作Controller
 * @author:wsc
 * @date:2020年6月11日 上午9:46:10
 */
@RestController
@RequestMapping("/practice/")
public class PracticeController {

	@Autowired
	PracticeService practiceService;

	@Autowired
	PracticeMapper practiceMapper;
	
	//通过学号获取学生实习记录列表
	@GetMapping("student-get-practicelist")
	public JSONObject queryPracticeListBySno(HttpSession session,String sno) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "操作成功";
		//如果为空代表，表示学生自己查询，否则为老师查询去学生
		if("".equals(sno) || sno==null) {
			sno = (String)session.getAttribute("account");
		}
		List<Practice> list = null;
		try {
			list = practiceService.queryPracticeListBySno(sno);
			json.put("list", list);
			json.put("status", status);
			json.put("message", message);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "操作失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
	}
	
	//通过实习编号id获取单个实习记录具体内容
	@SuppressWarnings("unchecked")
	@GetMapping("student-get-practiceInfo")
	public JSONObject queryPracticeInfoById(String practiceId) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "操作成功";
		
		try {
			Map<String, Object> map = practiceService.queryPracticeInfoById(practiceId);
			Practice practice = (Practice) map.get("practice");
			List<String> entryTestify = (List<String>) map.get("entryTestify");
			List<String> leaveTestify = (List<String>) map.get("leaveTestify");
			json.put("practice", practice);
			json.put("entryTestify", entryTestify);
			json.put("leaveTestify", leaveTestify);
			json.put("status", status);
			json.put("message", message);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "操作失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
	}
	
	
	/**
	 * 
	 * @description：添加或修改实习记录
	 * @param practiceId	实习记录id
	 * @param entryTime 入职时间
	 * @param companyId 实习公司Id
	 * @param job	岗位
	 * @param leaderName 直属领导
	 * @param leaderPhone	领导电话
	 * @param entryTestify	入职材料
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @return：
	 */
	@PostMapping("addOrUpdateEntryRecord")
	public JSONObject addPractice(@RequestParam("entryTestify") MultipartFile[] entryTestify,
			HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException {
		JSONObject json = new JSONObject();
		int status = 1;
		String message = "";
		// 获取登录学生的学号
		String sno = (String) session.getAttribute("account");
		//获取请求参数
//		String practiceId = request.getParameter("practiceId");
		String entryTime = request.getParameter("entryTime");
		String companyId = request.getParameter("companyId");
		String job = request.getParameter("job");
		String leaderName = request.getParameter("leaderName");
		String leaderPhone = request.getParameter("leaderPhone");
		if(entryTime==null) {
			message = "时间不能为空";
		}else if(companyId==null){
			message = "公司不能为空";
		}else if(job == null || "".equals(job)) {
			message = "岗位不能为空";
		}else if(leaderName == null || "".equals(leaderName)) {
			message = "领导姓名不能为空";
		}else if(leaderPhone == null || "".equals(leaderPhone)) {
			message = "领导电话不能为空";
		}else if(entryTestify == null || entryTestify.length==0) {
			message = "证明材料不能为空";
		}
		for(MultipartFile entry:entryTestify) {
			if(!FileUploadUtil.CheckImages(entry)) {
				message = "上传的图片格式不正确";
			}
		}
		try {
			if(message.length()==0) {
				
				job = job.replaceAll(" ", "");
				leaderName = leaderName.replaceAll(" ", "");
				leaderPhone = leaderPhone.replaceAll(" ", "");
				//获取该生的实习记录
				List<Practice> list = practiceMapper.queryPracticeListBySno(sno, null, null);
				//判断是否还处于就职状态
				if(list.size()>0) {
					Practice p = list.get(0);
					if(p.getLeaveTime()!=null && !"".equals(p.getLeaveTime())) {
						if(!(Integer.parseInt(entryTime.replaceAll("-", ""))>Integer.parseInt(p.getLeaveTime().replaceAll("-", "")) 
								|| Integer.parseInt(entryTime.replaceAll("-", ""))<Integer.parseInt(p.getEntryTime().replaceAll("-", "")))) {
							status = 4;
							message = "该时间段您还处于就职阶段，请选择其他日期！";
							json.put("status", status);
							json.put("message", message);
							return json;
						}
					}else {
						status = 2;
						message = "您还处于就职状态，不能提交新的入职登记！";
						json.put("status", status);
						json.put("message", message);
						return json;
					}
					boolean b = practiceService.addEntryOnPC(entryTime, companyId, sno, job, leaderName, leaderPhone, entryTestify, request, response);
					if(b) {
						status = 0;
						message = "添加成功";
						json.put("status", status);
						json.put("message", message);
						return json;
					}
				}else {
					boolean b = practiceService.addEntryOnPC(entryTime, companyId, sno, job, leaderName, leaderPhone, entryTestify, request, response);
					if(b) {
						status = 0;
						message = "添加成功";
						json.put("status", status);
						json.put("message", message);
						return json;
					}
				}
			}else {
				json.put("status", status);
				json.put("message", message);
				return json;
			}
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "操作失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		return json;
	}
	//删除实习记录
	@GetMapping("delete-practice")
	public JSONObject deletePractice(HttpServletRequest request,String practiceId) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "删除成功";
		boolean b = false;
		try {
			b = practiceService.deletePractice(request, practiceId);
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "删除失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		if(b) {
			json.put("status", status);
			json.put("message", message);
		}else {
			status = 1;
			message = "删除失败";
			json.put("status", status);
			json.put("message", message);
		}
		return json;
	}
	/**
	 * 
	 * @author: wsc
	 * @date:2020年6月17日 上午11:16:43
	 * @description：
	 * @param leaveTestify	离职材料
	 * @param practiceId	实习记录id
	 * @param leaveTime	离职时间
	 * @param leaveReason	离职原因
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 * @throws IOException：
	 */
	@PostMapping("updateLeaveRecord")
	public JSONObject updateLeaveRecord(@RequestParam("leaveTestify") MultipartFile[] leaveTestify, 
//			String practiceId,String leaveTime,String leaveReason,
			HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws IOException {
		JSONObject json = new JSONObject();
		String practiceId = request.getParameter("practiceId");
		String leaveTime = request.getParameter("leaveTime");
		String leaveReason = request.getParameter("leaveReason");
		String sno = (String) session.getAttribute("account");
		int status = 0;
		String message = "";
		if(leaveTime==null || "".equals(leaveTime)) {
			message = "时间不能为空";
		}else if(leaveReason==null || "".equals(leaveReason)) {
			message = "离职原因不能为空";
		}else if(leaveTestify==null) {
			message = "未选择离职证明材料";
		}
		for(MultipartFile leave:leaveTestify) {
			if(!FileUploadUtil.CheckImages(leave)) {
				message = "上传的图片格式不正确";
			}
		}
		//获取该生的实习记录
		Practice practice = practiceMapper.queryPracticeById(practiceId);
		if(practice==null) {
			message = "该实习记录已不存在，请先填写入职登记";
		}
		if(message.length()==0) {
			
			leaveReason = leaveReason.replaceAll(" " , "");
			
			List<Practice> list = practiceMapper.queryPracticeListBySno(sno, null, null);//判断所选的时间是否处于就职状态
			boolean b;
			try {
				if(list.size()>0) {
					for(Practice p: list) {
						if(p.getPracticeId().equals(practiceId)) {
							if(DateUtil.isDateCompare2(leaveTime, p.getEntryTime())) {
								status = 3;
								message = "离职时间要大于入职时间";
								json.put("status", status);
								json.put("message", message);
								return json;
							}else {
								continue;
							}
						}
						if(p.getLeaveTime()!=null && !"".equals(p.getLeaveTime())) {
							if(!(Integer.parseInt(leaveTime.replaceAll("-", ""))>Integer.parseInt(p.getLeaveTime().replaceAll("-", "")) 
									|| Integer.parseInt(leaveTime.replaceAll("-", ""))<Integer.parseInt(p.getEntryTime().replaceAll("-", "")))) {
								status = 4;
								message = "该时间段您还处于就职阶段，请选择其他日期！";
								json.put("status", status);
								json.put("message", message);
								return json;
							}
						}else {
							if(!DateUtil.isDateCompare2(leaveTime, p.getEntryTime())) {
								status = 4;
								message = "该时间段您还处于就职阶段，请选择其他日期！";
								json.put("status", status);
								json.put("message", message);
								return json;
							}
						}
					}
				}
				b = practiceService.addLeaveOnPC(practiceId, leaveTime, leaveReason, leaveTestify, request, response);
				if(b) {
					status = 0;
					message = "操作成功";
					json.put("status", status);
					json.put("message", message);
					return json;
				}
			} catch (Exception e) {
				e.printStackTrace();
				status = -1;
				message = "操作失败";
				json.put("status", status);
				json.put("message", message);
				return json;
			}
			return json;
		}else {
			status = 1;
			json.put("status", status);
			json.put("message", message);
			return json;
		}
	}
	//判断是否填写有入职登记但是没有填写离职登记的
	@GetMapping("isRecord")
	public JSONObject isRecord(HttpSession session,String sno) {

		JSONObject json = new JSONObject();
		int status = 0;	
		String message = "查询成功";
		if(sno==null) {
			sno = (String) session.getAttribute("account");
		}
		try {
			boolean b = practiceService.isRecord(sno);
			if(b) {
				List<Practice> list = practiceMapper.queryPracticeListBySno(sno,"is null",null);
				Practice practice = list.get(0);
				json.put("practiceId", practice.getPracticeId());
				json.put("entryTime", practice.getEntryTime());
				json.put("isRecord", 0);
				json.put("status", status);
				json.put("message", message);
				return json;
			}
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "操作失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		json.put("isRecord", 1);
		json.put("practiceId", "");
		json.put("entryTime", "");
		json.put("status", status);
		json.put("message", message);
		return json;
	}
}
